package com.zhxh.nzpos.goods;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import com.zhxh.nzpos.R;
import com.zhxh.nzpos.dao.CategoryDao;
import com.zhxh.nzpos.dao.GoodsDao;
import com.zhxh.nzpos.model.Category;
import com.zhxh.nzpos.model.Goods;
import com.zhxh.nzpos.utils.ViewUtil;

/**
 * 供货商管理
 * 
 * @author wangzt
 * 
 */
public class GoodsAddActivity extends Activity{


	private String[] category_items;
	List<Category> list;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_goods_add);
		
		initViews();

	}

	private void initViews() {
		Spinner category = (Spinner)findViewById(R.id.spinner1);
		
		CategoryDao cateDao = new CategoryDao(this);
		list =  cateDao.getAll();
		category_items = new String[list.size()+1];
		category_items[0] = "请选择分类";
		for(int i = 0 ; i < list.size() ;i++) {
			Category cate = list.get(i);
			category_items[i+1] = cate.getCategory();
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,category_items);
		 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		//adapter.setDropDownViewResource(R.layout.myspinnerdropdown);
		
		category.setAdapter(adapter);
	}


	public void add(View view) {
		EditText identifier = (EditText) findViewById(R.id.identifier);
		EditText name = (EditText) findViewById(R.id.name);
		//EditText category = (EditText) findViewById(R.id.category);
		EditText unit1 = (EditText) findViewById(R.id.unit1);
		EditText price1 = (EditText) findViewById(R.id.price1);
		EditText unit2 = (EditText) findViewById(R.id.unit2);
		EditText price2 = (EditText) findViewById(R.id.price2);
		EditText specification = (EditText) findViewById(R.id.specification);
		EditText points = (EditText) findViewById(R.id.points);
		EditText code = (EditText) findViewById(R.id.code);
		EditText note = (EditText) findViewById(R.id.note);
		boolean isOK = true;
		Spinner cateSpinner = (Spinner)findViewById(R.id.spinner1);
		int position = cateSpinner.getSelectedItemPosition();
		if( identifier.getText().toString().length() == 0 )
		{
			identifier.setError( "商品编码不能为空" );
			isOK = false;
		}
		if( name.getText().toString().length() == 0 ){
			name.setError( "商品名称不能为空" );
			isOK = false;
		}
			
		if( unit1.getText().toString().length() == 0 )
		{
			unit1.setError( "商品单位不能为空" );
			isOK = false;
		}
		if( price1.getText().toString().length() == 0 )
		{
			price1.setError( "商品价格不能为空" );
			isOK = false;
		}
		if( specification.getText().toString().length() == 0 )
		{
			specification.setError( "规格不能为空" );
			isOK = false;
		}
		if( points.getText().toString().length() == 0 )
		{
			points.setError( "积分不能为空" );
			isOK = false;
		}
		if( code.getText().toString().length() == 0 )
		{
			code.setError( "条码不能为空" );
			isOK = false;
		}
		
		if(position == 0) {			
			ViewUtil.makeToast(this, "请选择商品分类");
			isOK = false;
		}
		if(!isOK) return;
		
		Category cate = list.get(position-1);
		

		Goods goods = new Goods();
		goods.setId(identifier.getText().toString());
		goods.setCategoryId(cate.getId());
		goods.setCategoryName(cate.getCategory());
		goods.setCode(code.getText().toString());
		goods.setName(name.getText().toString());
		goods.setUnits1(unit1.getText().toString());
		goods.setUnits2(unit2.getText().toString());
		goods.setPrice1(Float.valueOf(price1.getText().toString()));
		goods.setPrice2(Float.valueOf(price2.getText().toString()));
		goods.setSpecification(specification.getText().toString());
		goods.setPonits(Integer.valueOf(points.getText().toString()));
		goods.setNote(note.getText().toString());
		goods.setType(1);
		GoodsDao dao = new GoodsDao(getApplicationContext());
		dao.save(goods);
        ViewUtil.makeToast(this, "添加商品信息成功");
		
		GoodsItem item = new GoodsItem();
		item.setId(goods.getId());
		item.setCategoryId(goods.getCategoryId());
		item.setCategoryName(goods.getCategoryName());
		item.setName(goods.getName());
		item.setNote(goods.getNote());
		item.setPrice1(goods.getPrice1());
		item.setPrice2(goods.getPrice2());
		item.setSpecification(goods.getSpecification());
		item.setUnits1(goods.getUnits1());
		item.setUnits2(goods.getUnits2());
		item.setCode(goods.getCode());
		item.setPoints(goods.getPonits());
		
		Intent i = new Intent();
		Bundle b = new Bundle();
		b.putSerializable("goods", item);
		i.putExtras(b);

		setResult(RESULT_OK, i);
		finish();

	}

	public void cancel(View view) {

		finish();

	}
}
